package hw;


import Util.Util;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Save extends JPanel implements ActionListener {
    private Atm atm;
    private Icon offImg = Util.getIcon("/buttonOff.jpg");
    private Icon sureImg = Util.getIcon("/buttonSure.jpg");
    private JButton  offBtn = new JButton(offImg);
    private JButton sureBtn = new JButton(sureImg);
    private JTextField inputMoney = new JTextField();

    public Save(Atm atm){
        this.atm = atm;
        offBtn.setBounds(930,200,140,50);
        sureBtn.setBounds(680,200,140,50);
        inputMoney.setBounds(430,200,240,50);

        //添加组件
        this.add(offBtn);
        this.add(sureBtn);
        this.add(inputMoney);
        //监听绑定
        offBtn.addActionListener(this);
        sureBtn.addActionListener(this);

        this.setLayout(null);
        this.setOpaque(false);

    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource()==offBtn){
            System.out.println("11");
            atm.switchPanel("menu");
        }
        if (e.getSource()==sureBtn){

            if (Integer.parseInt(inputMoney.getText())<0){
                JOptionPane.showConfirmDialog(this,"请输入正确的金额");
            }else {

                Users use = atm.getUseList().get(atm.getIndex());
                use.setBalance(use.getBalance()+Integer.parseInt( inputMoney.getText()));
                JOptionPane.showConfirmDialog(this,"存款正确");

            }


        }

    }
}
